package com.example.lark.im.controller;

import com.example.api.common.entity.EmptyData;
import com.example.api.common.utils.Result;
import com.example.lark.im.service.LarkSuiteService;
import com.example.lark.im.vo.resp.LarkSuiteJsapiTicketRespVO;
import com.lark.oapi.service.application.v6.model.SetAppBadgeReq;
import com.lark.oapi.service.contact.v3.model.BatchGetIdUserReq;
import com.lark.oapi.service.contact.v3.model.BatchGetIdUserRespBody;
import com.lark.oapi.service.contact.v3.model.UserContactInfo;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageRespBody;
import com.lark.oapi.service.im.v1.model.PatchMessageReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 飞书
 */
@RestController
@RequestMapping(value = "/larksuite")
public class LarkSuiteImController {

    @Autowired
    private LarkSuiteService larkSuiteService;

    @GetMapping("/getTenantAccessToken")
    public Result<String> getTenantAccessToken() {
        String tenantAccessToken = larkSuiteService.getTenantAccessToken();
        return Result.success(tenantAccessToken);
    }

    @PostMapping("/getJsapiTicket")
    public Result<LarkSuiteJsapiTicketRespVO> getJsapiTicket(@RequestParam("url") String url) {
        LarkSuiteJsapiTicketRespVO jsapiTicket = larkSuiteService.getJsapiTicket(url);
        return Result.success(jsapiTicket);
    }

    @PostMapping("/getUserIdByMobileOrEmail")
    public Result<List<UserContactInfo>> getUserIdByMobileOrEmail(@RequestBody BatchGetIdUserReq batchGetIdUserReq) {
        BatchGetIdUserRespBody batchGetIdUserRespBody = larkSuiteService.getUserIdByMobileOrEmail(batchGetIdUserReq);
        List<UserContactInfo> userContactInfos = Arrays.stream(batchGetIdUserRespBody.getUserList()).collect(Collectors.toList());
        return Result.success(userContactInfos);
    }

    @PostMapping("/sendMessage")
    public Result<CreateMessageRespBody> sendMessage(@RequestBody CreateMessageReq createMessageReq) {
        CreateMessageRespBody createMessageRespBody = larkSuiteService.sendMessage(createMessageReq);
        return Result.success(createMessageRespBody);
    }

    @PostMapping("/updateMessageCardFromApplication")
    public Result<EmptyData> updateMessageCardFromApplication(@RequestBody PatchMessageReq patchMessageReq) {
        larkSuiteService.updateMessageCardFromApplication(patchMessageReq);
        return Result.success();
    }

    @PostMapping("/updateAppBadge")
    public Result<EmptyData> updateAppBadge(@RequestBody SetAppBadgeReq setAppBadgeReq) {
        larkSuiteService.updateAppBadge(setAppBadgeReq);
        return Result.success();
    }

}
